//
//  SingerDetailsViewController.swift
//  ShareMusic
//
//  Created by 胡岩 on 16/5/4.
//  Copyright © 2016年 胡岩. All rights reserved.
//

import UIKit

class SingerDetailsViewController: SMBViewController, UITableViewDelegate, UITableViewDataSource, SMBTableViewDelegate {
    var tableView:SMBTableView?
    var page:Int = 1
    var lists:NSMutableArray = NSMutableArray.init(capacity: 0)
    var activityView:UIActivityIndicatorView?
    var parameters:[String: AnyObject]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.initView()
        self.network(parameters)
    }
    
    init(parameters:[String: AnyObject]?) {
        super.init(nibName: nil, bundle: nil)
        self.parameters = parameters
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func network(parameters:[String: AnyObject]?) {
        self.lists.removeAllObjects()
        self.tableView?.reloadData()
        self.activityView?.startAnimating()
        Network.networkGet(URLs.singerlistUrl, parameters: parameters) { (json) in
            if json["msg"] as! String == "ok" {
                let array = json["data"] as! NSArray
                
                for dic in array {
                    let model:SingerDetailsModel = SingerDetailsModel.init(dic: dic as? [String : AnyObject])
                    self.lists .addObject(model)
                }
                if (json["page"]! != nil) {
                    self.page = json["page"] as! Int
                }
                self.tableView?.reloadData()
                self.activityView?.stopAnimating()
            }
        }
    }
    
    func initView() {
        self.tableView = SMBTableView.init(frame: CGRectMake(0, 0, CGRect.screenWidth(), CGRect.screenHeight() - 64), style: UITableViewStyle.Plain)
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.separatorStyle = UITableViewCellSeparatorStyle.None
        self.view.addSubview(self.tableView!)
        self.tableView?.registerClass(SingerDetailsTableViewCell.self, forCellReuseIdentifier: "cell")
        self.activityView = UIActivityIndicatorView.init(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        self.activityView?.center = self.view.center
        self.view.addSubview(self.activityView!)
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:SingerDetailsTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SingerDetailsTableViewCell
        cell.model = self.lists.objectAtIndex(indexPath.row) as! SingerDetailsModel
        if indexPath.row % 2 == 0 {
            cell.shadow?.alpha = 0.1
        } else {
            cell.shadow?.alpha = 0.0
        }
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let model:SingerDetailsModel = self.lists[indexPath.row] as! SingerDetailsModel
        let songs:SongsViewController = SongsViewController()
        songs.parameters = ["page":1,"q":model.singer_name!,"size":50]
        songs.title = model.singer_name
        self.navigationController?.pushViewController(songs, animated: true)
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 70
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.lists.count
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
